#include "Bass.h"
#include "bassmidi.h"
#include <iostream>

//check for errors thrown by bass. returns the appropriate error message to stdout and 
//exits the application if an error has been thrown.
void checkError()
{
	int err = BASS_ErrorGetCode();

	switch(err)
	{
	case BASS_OK:
		std::cout << "BASS_OK" << std::endl;
		return;
	case BASS_ERROR_FILEOPEN:
		std::cout << "BASS_ERROR_FILEOPEN" << std::endl;
		exit(err);
	case -1:
		std::cout << "BASS_ERROR_UNKNOWN" << std::endl;
		exit(err);	
	default:
		std::cout<<"UNDEFINED ERROR CODE: " << err << std::endl ;
		exit(err);
	}
	//other error codes
	//1		BASS_ERROR_MEM
	//2		BASS_ERROR_FILEOPEN
	//3		BASS_ERROR_DRIVER
	//4		BASS_ERROR_BUFLOST
	//5		BASS_ERROR_HANDLE
	//6		BASS_ERROR_FORMAT
	//7		BASS_ERROR_POSITION
	//8		BASS_ERROR_INIT
	//9		BASS_ERROR_START
	//14	BASS_ERROR_ALREADY
	//18	BASS_ERROR_NOCHAN
	//19	BASS_ERROR_ILLTYPE
	//20	BASS_ERROR_ILLPARAM
	//21	BASS_ERROR_NO3D
	//22	BASS_ERROR_NOEAX
	//23	BASS_ERROR_DEVICE
	//24	BASS_ERROR_NOPLAY
	//25	BASS_ERROR_FREQ
	//27	BASS_ERROR_NOTFILE
	//29	BASS_ERROR_NOHW
	//31	BASS_ERROR_EMPTY
	//32	BASS_ERROR_NONET
	//33	BASS_ERROR_CREATE
	//34	BASS_ERROR_NOFX
	//37	BASS_ERROR_NOTAVAIL
	//38	BASS_ERROR_DECODE
	//39	BASS_ERROR_DX
	//40	BASS_ERROR_TIMEOUT
	//41	BASS_ERROR_FILEFORM
	//42	BASS_ERROR_SPEAKER
	//43	BASS_ERROR_VERSION
	//44	BASS_ERROR_CODEC
	//45	BASS_ERROR_ENDED
	//46	BASS_ERROR_BUSY
}

/**
 * http://www.un4seen.com/doc/
**/
int main(int argc, char** argv)
{
	int stream;
	DWORD autoChooseSoundFont = 0; //0 = off, 2 = all channels
	char* midiFile = "james_bond_loop2.mid";
	char* defaultSoundFont = "SYNTHGMS.SF2";
	char* soundFont = "SYNTHGMS.SF2";

	//initialise the bass library
	BASS_Init(-1, 44100, BASS_DEVICE_DEFAULT, 0, NULL);
	checkError();

	//load the bass midi file
	BASS_PluginLoad("bassmidi.dll", 0);
	checkError();	

	//automatically choose soundfont based on filename?
	BASS_SetConfig(BASS_CONFIG_MIDI_AUTOFONT, autoChooseSoundFont);
	checkError();

	//default soundfont
	BASS_SetConfigPtr(BASS_CONFIG_MIDI_DEFFONT, defaultSoundFont);
	checkError();

	stream = BASS_StreamCreateFile(false, midiFile, 0, 0, BASS_STREAM_AUTOFREE | BASS_SAMPLE_SOFTWARE | BASS_SAMPLE_LOOP);
	checkError();

	//load soundfont
	BASS_MIDI_FONT fonts[1];
	fonts[0].font=BASS_MIDI_FontInit(soundFont, 0);
	checkError();
	fonts[0].preset=-1; // for all presets
	fonts[0].bank=0; // default bank 0

	//apply soundfont to stream
	BASS_MIDI_StreamSetFonts(stream, fonts, 1); 
	checkError();

	//play the stream
	BASS_ChannelPlay(stream, true);
	checkError();	

	std::cout << "Playing." << std::endl;
	char result;
	std::cin >> result;

	return 0;
}

    /*Private Stream As Integer
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
      
        Bass.BASS_Init(-1, 44100, BASSInit.BASS_DEVICE_DEFAULT, Me.Handle)
      
        Bass.BASS_PluginLoad("bassmidi.dll")
         BassMidi.BASS_MIDI_FontInit("C:\Users\Stephan\Desktop\brauchbar+\Giga Piano.sf2")
        Stream = BassMidi.BASS_MIDI_StreamCreateFile("C:\Users\Stephan\Documents\Harmonielehre\Harmonielehre\quintparallelen.mid", 0, 0, BASSFlag.BASS_DEFAULT, 44100)
        BassMidi.BASS_MIDI_StreamSetFonts(Stream, ?, ?)
        Bass.BASS_ChannelPlay(Stream, True)
    End Sub*/